package com.bignerdranch.android.remotecontrol;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

/**
 * Created by ruben.gonzalez on 29/05/14.
 */
public class RemoteControlFragment extends Fragment {
    private TextView mWorkingTextView;
    private TextView mSelectedTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_remote_control, container, false);

        mWorkingTextView = (TextView) v.findViewById(R.id.fragment_remote_control_workingTextView);
        mSelectedTextView = (TextView) v.findViewById(R.id.fragment_remote_control_selectedTextView);

        View.OnClickListener numberClickListener = new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                TextView textView = (TextView) view;
                String working = mWorkingTextView.getText().toString();
                String text = textView.getText().toString();

                if(working.equals("0")){
                    mWorkingTextView.setText(text);
                }else{
                    mWorkingTextView.setText(working + text);
                }
            }
        };

        // Set numbers for buttons
        TableLayout tableLayout = (TableLayout) v.findViewById(R.id.fragment_remote_control_tableLayout);
        int n = 1;
        for (int i = 2; i < tableLayout.getChildCount() - 1; i++){
            TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
            for (int j = 0; j < tableRow.getChildCount(); j++){
                Button button = (Button) tableRow.getChildAt(j);
                button.setText("" + n);
                button.setOnClickListener(numberClickListener);
                n++;
            }
        }

        // Set last row Delete and Enter and 0
        TableRow bottomRow = (TableRow) tableLayout.getChildAt(tableLayout.getChildCount() - 1);
        Button deleteButton = (Button) bottomRow.getChildAt(0);
        deleteButton.setText("Delete");
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWorkingTextView.setText("0");
            }
        });

        Button zeroButton = (Button) bottomRow.getChildAt(1);
        zeroButton.setText("0");

        Button enter = (Button) bottomRow.getChildAt(2);
        enter.setText("Enter");
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CharSequence working =mWorkingTextView.getText();

                if(working.length() > 0){
                    mSelectedTextView.setText(working);
                }
                mWorkingTextView.setText("0");
            }
        });

        return v;
    }
}
